﻿
using BgWorks.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System.Net;

namespace BgWorks.Filters
{
    public class CustomExceptionAttribute : IExceptionFilter
    {
        private readonly ILogger<CustomExceptionAttribute> _logger;
        public CustomExceptionAttribute(ILogger<CustomExceptionAttribute> logger)
        {
            _logger = logger;
        }

        public void OnException(ExceptionContext context)
        {
            _logger.LogError( context.Exception.Message);
            _logger.LogError( context.Exception.StackTrace);
            var status = HttpStatusCode.InternalServerError;
            context.ExceptionHandled = true;
            context.Result = new ObjectResult(new ResponseApi<string> { code = (int)status, msg = context.Exception.Message });
        }
    }
}
